//
//  UIButton-Exteration.swift
//  ZHFWB
//
//  Created by 张海峰 on 2017/7/7.
//  Copyright © 2017年 张海峰. All rights reserved.
//

import Foundation
import UIKit
extension UIButton{
    //swift 中类方法是以class 开头的方法，类似于OC中 +开头的方法
    class func creatButton(imageName : String, bgImageName : String) -> UIButton {
        let btn = UIButton()
        btn.setImage(UIImage.init(named: imageName), for: .normal)
        btn.setImage(UIImage.init(named: imageName + "_highlighted"), for: .highlighted)
        btn.setImage(UIImage.init(named: imageName + "_highlighted"), for: .selected)
        btn.setBackgroundImage(UIImage.init(named: bgImageName), for: .normal)
        btn.setBackgroundImage(UIImage.init(named: bgImageName + "_highlighted"), for: .highlighted)
        btn.setBackgroundImage(UIImage.init(named: bgImageName + "_highlighted"), for: .selected)
      
        btn.sizeToFit()
        return btn
    }
    /*
     1.便利构造函数用在类扩展（extension）里面
     2.便利构造函数init前面加 convenience
     3.在便利构造函数中需要明确的调用self.init()
     */
    
    convenience init(imageName :String, bgImageName :String) {
        self.init()
        setImage(UIImage.init(named: imageName), for: .normal)
        setImage(UIImage.init(named: imageName + "_highlighted"), for: .selected)
        setBackgroundImage(UIImage.init(named: bgImageName), for: .normal)
        setBackgroundImage(UIImage.init(named: bgImageName + "_highlighted"), for: .selected)
        sizeToFit()//自适应，如果不添加宽高都是0
    }

}
